package com.tengju.bff.interfaces.manage.crm.dashborad;

import com.tengju.bff.interfaces.app.domination.vo.StarLevelNumVO;
import com.tengju.bff.interfaces.manage.crm.dashborad.facade.DashboardDominationTeamFacade;
import com.tengju.bff.interfaces.manage.crm.dashborad.facade.PerformanceNewFacade;
import com.tengju.bff.interfaces.manage.crm.dashborad.facade.DashboardPeopleAnalysisFacade;
import com.tengju.bff.interfaces.manage.crm.dashborad.facade.DashboardMarketSortFacade;
import com.tengju.bff.interfaces.manage.crm.dashborad.vo.PerformanceVO;
import com.tengju.bff.interfaces.shared.*;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.data.domain.model.user.DominationLevelEnum;
import com.tengju.user.domain.model.user.UserIdCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@Api(tags = "生意参谋-仪表盘管理")
@RequestMapping(value = "manage/crm/dashboard/new")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class DashboardNewController {

    private final PerformanceNewFacade performanceNewFacade;

    private final DashboardMarketSortFacade dashboardMarketSortFacade;

    private final DashboardPeopleAnalysisFacade dashboardPeopleAnalysisFacade;

    private final DashboardDominationTeamFacade dashboardDominationTeamFacade;


    @ApiOperation(value = "我的目标（KA）", notes = "我的目标（KA）")
    @RequestMapping(value = "/ka/target", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    //todo 目标优化
    public ApiResponse<List<KVVO>> kaTarget(@NotNull(message = "日期类型不能为空") @RequestParam("dateType") Integer dateType,
                                               @NotBlank(message = "日期不能为空") @RequestParam("date")  String date) {
        return ApiResponse.newSuccess(performanceNewFacade.kaTarget(dateType, date,"motivation_target_dashboard"));
    }

    @ApiOperation(value = "我的目标（SEC）", notes = "我的目标（SEC）")
    @RequestMapping(value = "/sec/target", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    //todo 目标优化
    public ApiResponse<List<KVVO>> secTarget(@NotNull(message = "日期类型不能为空") @RequestParam("dateType") Integer dateType,
                                                    @NotBlank(message = "日期不能为空") @RequestParam("date") String date,
                                                    @NotNull(message = "SEC的idCode不能为空") @RequestParam("idCode") Long idCode) {
        return ApiResponse.newSuccess(performanceNewFacade.secTarget(dateType, date, List.of(new UserIdCode(idCode)),"motivation_target_sec_dashboard"));
    }

    @ApiOperation(value = "我的目标(SEC目标表格)", notes = "我的目标（SEC目标表格）")
    @RequestMapping(value = "/sec/targetTable", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    //todo 目标优化
    public ApiResponse<TableVO> secTargetTable(@NotNull(message = "日期类型不能为空") @RequestParam("dateType")  Integer dateType,
                                                  @NotBlank(message = "日期不能为空") @RequestParam("date") String date) {
        return ApiResponse.newSuccess(performanceNewFacade.secTargetTable(dateType, date));
    }

    @ApiOperation(value = "工作台(KA)品牌销售榜", notes = "工作台(KA)品牌销售榜")
    @RequestMapping(value = "/market/getBrandMarketSortInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> getBrandMarketSortInfo(@NotNull(message = "日期类型不能为空") @RequestParam Integer dateType) {
        TableVO vo = dashboardMarketSortFacade.getKABrandMarketSortInfo(dateType);
        return ApiResponse.newSuccess(vo);

    }

    @ApiOperation(value = "工作台(SEC)品牌销售榜", notes = "工作台(SEC)品牌销售榜")
    @RequestMapping(value = "/market/getSECBrandMarketSortInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> getSECBrandMarketSortInfo(@NotNull(message = "日期类型不能为空") @RequestParam Integer dateType,
                                                          @NotNull(message = "SEC的idCode不能为空") @RequestParam Long idCode) {
        TableVO vo = dashboardMarketSortFacade.getSECBrandMarketSortInfo(idCode, dateType);
        return ApiResponse.newSuccess(vo);
    }

    @ApiOperation(value = "工作台(SEC)商品销售榜", notes = "工作台(SEC)商品销售榜")
    @RequestMapping(value = "/market/getGoodsMarketSortInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> getGoodsMarketSortInfo(@NotNull(message = "日期类型不能为空") @RequestParam Integer dateType,
                                                       @NotNull(message = "SEC的idCode不能为空") @RequestParam Long idCode) {
        TableVO vo = dashboardMarketSortFacade.getSECGoodsmarketSortinfo(idCode, dateType);
        return ApiResponse.newSuccess(vo);
    }

    @ApiOperation(value = "工作台(KA)人群分析", notes = "工作台(KA)人群分析")
    @RequestMapping(value = "/analysis/getKAPeopleAnalysisInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO>> getKAPeopleAnalysisInfo() {
        List<KVVO> kvvos = dashboardPeopleAnalysisFacade.getKAPeopleAnalysisInfo();
        return ApiResponse.newSuccess(kvvos);

    }

    @ApiOperation(value = "工作台(SEC)人群分析", notes = "工作台(SEC)人群分析")
    @RequestMapping(value = "/analysis/getSECPeopleAnalysisInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO>> getSECPeopleAnalysisInfo(@NotNull(message = "SEC的idCode不能为空") @RequestParam Long idCode) {
        List<KVVO> kvvos = dashboardPeopleAnalysisFacade.getSECPeopleAnalysisInfo(idCode);
        return ApiResponse.newSuccess(kvvos);

    }

    @ApiOperation(value = "工作台(KA)新增服务商", notes = "工作台(KA)新增服务商")
    @RequestMapping(value = "/sec/domination/memberNewNum", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<StarLevelNumVO>> memberNewNum() {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        List<com.tengju.data.domain.model.user.UserIdCode> dataGenSecIdCodes = servletContext.getDataGenSecIdCodes();
        return ApiResponse.newSuccess(dashboardDominationTeamFacade
                .memberNewNum(dataGenSecIdCodes, DominationLevelEnum.GEN_SEC));
    }

    @ApiOperation(value = "工作台(KA)签约中服务商", notes = "工作台(KA)签约中服务商")
    @RequestMapping(value = "/sec/domination/memberSigning", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<StarLevelNumVO>> memberSigning() {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        List<com.tengju.data.domain.model.user.UserIdCode> dataGenSecIdCodes = servletContext.getDataGenSecIdCodes();
        return ApiResponse.newSuccess(dashboardDominationTeamFacade
                .signingMember(dataGenSecIdCodes, DominationLevelEnum.GEN_SEC));
    }


    @ApiOperation(value = "工作台(KA)即将晋升", notes = "工作台(KA)即将晋升")
    @RequestMapping(value = "/sec/domination/upComing", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<StarLevelNumVO>> upComing() {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        List<com.tengju.data.domain.model.user.UserIdCode> dataGenSecIdCodes = servletContext.getDataGenSecIdCodes();
        return ApiResponse.newSuccess(dashboardDominationTeamFacade
                .upComing(dataGenSecIdCodes, DominationLevelEnum.GEN_SEC));
    }
}
